package com.heimali.happyreader.utils;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Iterator;
import java.util.Map;

import org.apache.http.client.utils.URIUtils;

import com.heimali.happyreader.config.Constants;

public class HttpUtils {
	public static String buildQuery(Map<String, String> map) {
		String res = "";
		Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
		while(it.hasNext()) {
			Map.Entry<String, String> entry = (Map.Entry<String, String>) it.next();
			res += entry.getKey() + "=" + entry.getValue() + "&";
		}
		return res;
	}
	
	public static String buildQuery(String[]... args) {
		String res = "";
		for(String[] item : args) {
			res += item[0] + "=" + item[1] + "&";
		}
		return res;
	}
	
	public static URI buildURI(String path, String query) {
		try {
			return URIUtils.createURI(Constants.PROTOCOL, Constants.HOST, Constants.PORT, path, query, null);
		} catch (URISyntaxException e) {
			e.printStackTrace();
			return null;
		}
	}
}
